package com.ucode.wap.hrm.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.ucode.hrm.service.HrmEmployeeContractService;
import com.ucode.model.hrm.HrmEmployeeContract;
import com.ucode.model.hrm.convert.HrmEmployeeContractConvert;
import com.ucode.model.hrm.vo.HrmEmployeeContractSimpleVO;
import com.ucode.tool.annotation.NeedLogin;
import com.ucode.tool.base.ResponseResult;
import com.ucode.wap.hrm.service.ApiHrmEmployeeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Tag(name = "移动端 - 人力资源系统 - 员工")
@RestController
@RequestMapping("/hrm/employee-contract")
@Validated
public class ApiHrmEmployeeContractController {

    private static Log log = LogFactory.get();

    @Resource
    private HrmEmployeeContractService employeeContractService;
    @Resource
    private ApiHrmEmployeeService apiHrmEmployeeService;

    @NeedLogin
    @GetMapping("/contract-list")
    @Operation(summary = "合同列表")
    public ResponseResult<List<HrmEmployeeContractSimpleVO>> contractList(){
        List<HrmEmployeeContract> list =  employeeContractService.queryListByEmployeeId(apiHrmEmployeeService.verifyCurrentUserBindAndGetEmployeeId(),true);
        return ResponseResult.success(HrmEmployeeContractConvert.INSTANCE.convertSimpleList(list));
    }


    @NeedLogin
    @GetMapping("/sign-url/{id}")
    @Parameter(description="主键ID",name = "id", required = true,in = ParameterIn.PATH,example = "1024")
    @Operation(summary = "获取签署地址")
    public ResponseResult<String> sign(@PathVariable(name="id",required=true) Long id){
        Long employeeId = apiHrmEmployeeService.verifyCurrentUserBindAndGetEmployeeId();
        String signUrl = employeeContractService.generateThreeContract(id,employeeId);
        return ResponseResult.success(signUrl);
    }



    @PostMapping("/sign-notify")
    @Operation(summary = "合同签署回调")
    public ResponseResult<Boolean> signNotify(
            @Schema(description = "交易号") @RequestParam(value = "transaction_id",required = true) String transaction_id,
            @Schema(description = "合同编号") @RequestParam(value = "contract_id",required = true) String contract_id,
            @Schema(description = "签章结果代码:3000（签章成功）3001（签章失败）3003（拒签）") @RequestParam(value = "result_code",required = true) String result_code,
            @Schema(description = "签章结果描述") @RequestParam(value = "result_desc",required = true) String result_desc,
            @Schema(description = "下载地址") @RequestParam(value = "download_url",required = false) String download_url,
            @Schema(description = "查看地址") @RequestParam(value = "pageSize",required = false) String viewpdf_url,
            @Schema(description = "请求时间") @RequestParam(value = "timestamp",required = true) String timestamp,
            @Schema(description = "摘要") @RequestParam(value = "msg_digest",required = true) String msg_digest){

        //注意：msg_digest=》 Base64(SHA1(app_id +MD5(timestamp)+SHA1(app_secret+transaction_id))  建议接入平台方进行校验

        log.info("合同签署回调参数transaction_id:{},contract_id:{},result_code:{},result_desc:{},download_url:{},viewpdf_url:{},timestamp:{},msg_digest:{}",transaction_id,contract_id,result_code,result_desc,download_url,viewpdf_url,timestamp,msg_digest);

        if("3000".equals(result_code)) {
            employeeContractService.signNotify(transaction_id,Long.valueOf(contract_id),download_url,viewpdf_url);
        }
        return ResponseResult.success();
    }


    @PostMapping("/auth-notify")
    @Operation(summary = "实名认证回调")
    public ResponseResult<Boolean> notify(
            @Schema(description = "平台方id") @RequestParam(value = "appId",required = true) String appId,
            @Schema(description = "认证序列号") @RequestParam(value = "serialNo",required = true) String serialNo,
            @Schema(description = "客户编号") @RequestParam(value = "customerId",required = true) String customerId,
            @Schema(description = "个人Status") @RequestParam(value = "status",required = true) String status,
            @Schema(description = "不通过原因描述",example="1") @RequestParam(value = "statusDesc",required = true) String statusDesc,
            @Schema(description = "实名绑定信息",example="5") @RequestParam(value = "certStatus",required = false) String certStatus,
            @Schema(description = "帐号类型",example="5") @RequestParam(value = "authenticationType",required = false) String authenticationType,
            @Schema(description = "请求时间",example="1") @RequestParam(value = "timestamp",required = true) String timestamp,
            @Schema(description = "摘要",example="5") @RequestParam(value = "msg_digest",required = true) String msg_digest){

        //注意：msg_digest=》 Base64(SHA1(app_id +MD5(timestamp)+SHA1(app_secret+transaction_id))  建议接入平台方进行校验

        log.info("实名认证回调参数appId:{},serialNo:{},customerId:{},status:{},statusDesc:{},certStatus:{},authenticationType:{},timestamp:{},msg_digest:{}",appId,serialNo,customerId,status,statusDesc,certStatus,authenticationType,timestamp,msg_digest);

        return ResponseResult.success();
    }

}
